package com.boot.config.datasource

import com.alibaba.druid.pool.DruidDataSource
import com.boot.config.ParamConfig
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import java.util.HashMap

import javax.sql.DataSource

/**
 * @author kawins
 * createTime 2018-12-07 10:59
 */
@Configuration
class DataSources {

    @Autowired
    private lateinit var paramConfig: ParamConfig

    @Primary
    @Bean("dataSource")
    fun dataSource(@Qualifier("mysqlDataSource") mysqlDataSource: DataSource,
                   @Qualifier("oracleDataSource") oracleDataSource: DataSource): DynamicDataSource {
        val targetDataSources = HashMap<Any, Any>()
        targetDataSources[DataName.MYSQL] = mysqlDataSource
        targetDataSources[DataName.ORACLE] = oracleDataSource
        val dataSource = DynamicDataSource()
        dataSource.setTargetDataSources(targetDataSources)
        dataSource.setDefaultTargetDataSource(mysqlDataSource)
        return dataSource
    }

    @Bean("mysqlDataSource")
    fun mysqlDataSource(): DataSource {
        return DruidDataSource().apply {
            url = paramConfig.mysqlSource.url
            username = paramConfig.mysqlSource.username
            password = paramConfig.mysqlSource.password
            driverClassName = paramConfig.mysqlSource.driverClassName
            initialSize = paramConfig.mysqlSource.druidInitialSize
            maxActive = paramConfig.mysqlSource.druidMaxActive
            minIdle = paramConfig.mysqlSource.druidMinIdle
            maxWait = paramConfig.mysqlSource.druidMaxWait
            timeBetweenEvictionRunsMillis = paramConfig.mysqlSource.druidTimeBetweenEvictionRunsMillis
            minEvictableIdleTimeMillis = paramConfig.mysqlSource.druidMinEvictableIdleTimeMillis
            validationQuery = paramConfig.mysqlSource.druidValidationQuery
            isTestWhileIdle = paramConfig.mysqlSource.druidTestWhileIdle
            isTestOnReturn = paramConfig.mysqlSource.druidTestOnReturn
            isTestOnBorrow = paramConfig.mysqlSource.druidTestOnBorrow
            isPoolPreparedStatements = paramConfig.mysqlSource.druidPoolPreparedStatements
            maxPoolPreparedStatementPerConnectionSize = paramConfig.mysqlSource.druidMaxPoolPreparedStatementPerConnectionSize
        }
    }

    /**
     * 
     */
    @Bean("oracleDataSource")
    fun oracleDataSource(): DataSource {
        return DruidDataSource().apply {
            url = paramConfig.oracleSource.url
            username = paramConfig.oracleSource.username
            password = paramConfig.oracleSource.password
            driverClassName = paramConfig.oracleSource.driverClassName
            initialSize = paramConfig.oracleSource.druidInitialSize
            maxActive = paramConfig.oracleSource.druidMaxActive
            minIdle = paramConfig.oracleSource.druidMinIdle
            maxWait = paramConfig.oracleSource.druidMaxWait
            timeBetweenEvictionRunsMillis = paramConfig.oracleSource.druidTimeBetweenEvictionRunsMillis
            minEvictableIdleTimeMillis = paramConfig.oracleSource.druidMinEvictableIdleTimeMillis
            validationQuery = paramConfig.oracleSource.druidValidationQuery
            isTestWhileIdle = paramConfig.oracleSource.druidTestWhileIdle
            isTestOnReturn = paramConfig.oracleSource.druidTestOnReturn
            isTestOnBorrow = paramConfig.oracleSource.druidTestOnBorrow
            isPoolPreparedStatements = paramConfig.oracleSource.druidPoolPreparedStatements
            maxPoolPreparedStatementPerConnectionSize = paramConfig.oracleSource.druidMaxPoolPreparedStatementPerConnectionSize
        }
    }
}
